<template>
  <div v-if="this.itemArr" class="discount-container">
    <s-discount v-if="showSkeleton"></s-discount>
    <ul v-else v-infinite-scroll="requestData" infinite-scroll-disabled="cellLoading" infinite-scroll-distance="0" infinite-scroll-immediate-check="false">
        <router-link tag="li" v-for="item in itemArr" :to=" 'details/' + item.id">
          <my-image class="my-img fade" :imgUrl="item.imgUrl"></my-image>
          <div class="right">
              <div class="right-top">
                <div class="name">{{item.itemName}}</div>
                <div class="dec">{{item.dec}}</div>
              </div>
              
              <div class="price-div">
                <span class="symbol">&yen;</span><span class="price">{{item.price}}</span><span class="word">元</span>
              </div>
          </div>
        </router-link>
    </ul>
  </div>
</template>

<script>
import { mapState } from "vuex";
import myImage from "../../lazyloadImg/lazyImage.vue";
import Vue from "vue";
import { InfiniteScroll } from "mint-ui";
import sDiscount from "../../skeletons/sDiscount.vue";
Vue.use(InfiniteScroll);

export default {
  name: "discount-combine",
  data() {
    return {
      itemArr: [],
      cellLoading: true,
      curPage: 0,
      pageNum: 10,
      existIdArr: [],
      isLoadDb: false,
      showSkeleton: true,
      clearId: -1000
    };
  },
  components: {
    myImage,
    sDiscount
  },
  activated() {},
  mounted() {
    this.clearId = setTimeout(() => {
      clearTimeout(this.clearId);
      this.clearId = -1000;
      if (this.isLoadDb) this.showSkeleton = false;
    }, 300);
    this.requestData(false);
  },
  methods: {
    requestData(infinite = true) {
      this.$store.dispatch("toggleLoading", true);
      this.$http
        .post(this.path + "store/Goods/goods_list", {
          type: "2",
          page: this.curPage,
          storeid: this.storeid
        })
        .then(response => {
          this.$store.dispatch("toggleLoading", false);
          if (infinite && this.page == 0) return;
          if (infinite && this.cellLoading) return;
          this.cellLoading = true;
          if (!this.isLoadDb) {
            this.isLoadDb = true;
            //如果过了400ms，移除骨架页面
            if (this.clearId == -1000) this.showSkeleton = false;
          }
          let serverData = response.body.result;
          if (!serverData || serverData.length == 0) {
            //数据为空
            this.cellLoading = true;
          } else {
            //有数据
            if (serverData.length % 10 == 0) {
              //满一页数据
              this.curPage++;
              this.cellLoading = false;
            } else {
              //不满一页
              this.cellLoading = true;
            }
            let detailsArr = this.analysisData(serverData);
            for (let i = 0; i < detailsArr.length; i++) {
              let orderid = detailsArr[i].id;
              if (this.existIdArr.indexOf(orderid) >= 0) continue;
              this.itemArr.push(detailsArr[i]);
              this.existIdArr.push(orderid);
            }
          }
        })
        .catch(response => {
          console.log(response);
          this.$store.dispatch("toggleLoading", false);
        });
    },
    analysisData(data) {
      let detailsArr = [];
      data.forEach((val, index, arr) => {
        detailsArr.push({
          id: val.id,
          type: "0",
          imgUrl: this.soursePath + val.icon,
          itemName: val.gname,
          price: val.price,
          dec: val.message,
          discount: val.discount
        });
      });
      return detailsArr;
    }
  },
  computed: {
    ...mapState(["openid", "path", "userid", "storeid", "soursePath"])
  }
};
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
.discount-container {
  background-color: #efeff0;
  width: 100%;
  flex: 1;
  padding-bottom: 0.41rem;

  ul {
    padding: 0.1rem 0.1rem 0 0.1rem;
    box-sizing: border-box;

    li {
      width: 100%;
      height: 1rem;
      padding: 0.1rem;
      box-sizing: border-box;
      margin-bottom: 0.08rem;
      position: relative;
      background-color: #eb9eb3;
      display: flex;

      .my-img {
        flex-shrink: 0;
        background-color: #fff;
        width: 0.8rem;
        height: 0.8rem;
      }

      .right {
        flex: 1;
        margin-left: 0.14rem;
        display: flex;
        flex-direction: column;

        .right-top {
          flex: 1;

          .name {
            font-size: 0.13rem;
            color: #fff;
            letter-spacing: 0.01rem;
          }

          .dec {
            font-size: 0.12rem;
            color: #595656;
            margin-top: 0.06rem;
            line-height: 0.16rem;
          }
        }

        .price-div {
          color: #fff;
          align-self: flex-end;

          .symbol {
            font-size: 0.12rem;
            margin-right: 0.01rem;
          }

          .price {
            font-size: 0.13rem;
          }

          .word {
            font-size: 0.13rem;
            margin-left: 0.01rem;
          }
        }
      }
    }
  }
}
</style>
